/**
 * 08/dic/2011 - fabio.melillo@gmail.com
 */
package demo.unisannio;

import org.unisannio.remote.GenericRemote;
import org.unisannio.remote.RemoteContentProvider;
import org.unisannio.util.MovieMetaData;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;

/**
 * @author fabio
 * 
 */
public class InsertMoviesTask extends AsyncTask<GenericRemote, Integer, Uri> {

	private Activity context;
	private ProgressDialog dialog;
	private ContentValues cv;

	public InsertMoviesTask(Activity activity,ContentValues cv) {
		this.context = activity;
		this.cv=cv;
	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(context, "",
				"Querying. Please wait...", true);
	}

	@Override
	protected Uri doInBackground(GenericRemote... remote) {
		Uri uri=MovieMetaData.CONTENT_URI;
//		ContentValues contentValues=new ContentValues();
//		contentValues.put(MovieMetaData.MOVIE_DIRECTOR_KEY, "test"+Random.nextInt(50));
//		contentValues.put(MovieMetaData.MOVIE_TITLE_KEY, "Zelig_"+Random.nextInt(100));
//		contentValues.put(MovieMetaData.MOVIE_YEAR_KEY, 1000+Random.nextInt(1000));
//		contentValues.put(MovieMetaData.MOVIE_RATE_KEY, 1+Random.nextInt(4));
//        sendOrderedBroadcast(intent, null, clientRec, null, Activity.RESULT_OK, null, null);
		Uri result=((RemoteContentProvider) remote[0]).insert(uri, cv);
		
		return result;
	}

	@Override
	protected void onPostExecute(Uri uri) {
		dialog.dismiss();
		Toast toast;
		
	 	dialog.dismiss();
		if (uri == null) {
			toast = Toast.makeText(context,
					"Il Peer selezionato potrebbe essersi allontanato",
					Toast.LENGTH_LONG);
			toast.show();
		} else {
			toast = Toast.makeText(context,"Uri: "+uri.toString(),Toast.LENGTH_SHORT);
		 	toast.show();
		}
	}

}
